package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.Storage;
import com.cskaoyan.bean.wx.storage.vo.StorageVo;
import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.service.wx.WxStorageService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

/**
 * @Name : WxStorageServiceImpl.java
 * @Time : 2021/8/14 15:03
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@Transactional
@Service
public class WxStorageServiceImpl implements WxStorageService {

    @Autowired
    StorageMapper storageMapper;

    @Autowired
    AliyunComponent aliyunComponent;

    @SneakyThrows
    @Override
    public StorageVo upload(MultipartFile uploadFile) {
        UUID uuid = UUID.randomUUID();
        String originalFilename = uploadFile.getOriginalFilename();
        String contentType = uploadFile.getContentType();
        String[] split = contentType.split("/");
        String key = (uuid + "." + split[1]).replaceAll("-","");
        int hashCode = originalFilename.hashCode();
        String hexString = Integer.toHexString(hashCode);
//        String relativePath = "wxImg";
//        char[] chars = hexString.toCharArray();
//        for (char aChar : chars) {
//            relativePath = relativePath + "/" + aChar;
//        }
//        relativePath = relativePath + key;
        Integer size = ((int) uploadFile.getSize());
        String bucket = aliyunComponent.getOss().getBucket();
        String endPoint = aliyunComponent.getOss().getEndPoint();
        String url = "https://" + bucket + "." + endPoint + "/" + key;
        Date now = new Date(System.currentTimeMillis());


//        String path = ClassUtils.getDefaultClassLoader().getResource("static").getPath();
//        File file = new File(path + "/" + relativePath);
//        if (!file.getParentFile().exists()) {
//            file.getParentFile().mkdirs();
//        }
//        uploadFile.transferTo(file);

        aliyunComponent.fileUpload(key,uploadFile.getInputStream());

        Storage storage = new Storage();
        storage.setKey(key);
        storage.setName(originalFilename);
        storage.setType(contentType);
        storage.setSize(size);
        storage.setUrl(url);
        storage.setAddTime(now);
        storage.setUpdateTime(now);
        storage.setDeleted(false);
        storageMapper.insert(storage);

        StorageVo storageVo = new StorageVo();
        storageVo.setId(storage.getId());
        storageVo.setKey(storage.getKey());
        storageVo.setName(storage.getName());
        storageVo.setType(storage.getType());
        storageVo.setSize(storage.getSize());
        storageVo.setUrl(storage.getUrl());
        storageVo.setAddTime(storage.getAddTime());
        storageVo.setUpdateTime(storage.getUpdateTime());
        return storageVo;
    }
}
